package a66weding.com.jiehuntong.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MeAlterPhoneBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.CacheUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * 绑定新手机设置手机验证码界面
 */
public class MeNewSetBindnumActivity extends MeBaseActivity implements View.OnClickListener, OnNetResultListener {
    private LinearLayout mBackLay;
    private TextView mBackContentTv;
    private TextView mHeaderTitleTv;
    private RelativeLayout mHeaderRightRlay;
    private Button mFinishBtn;
    private TextView mCodeTv;
    private EditText mNewCodeEt;
    private String newPhoneNumber;
    private String verificationCode;
    private String newCode;
    private HttpRequestBean httpRequestBean;
    private TextView mTimeTv;
    private int recLen = 60;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_set_new_bindnum);
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("MeNewSetBindnumActivity", this);//全局的Activity的管理类
        ((GlobalApplication) getApplication()).getMeActivityManager().addActivity("MeNewSetBindnumActivity", this);//获取到我的Acivity管理类,向里添加

        initview();
    }

    private void initview() {
        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mBackContentTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mHeaderTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mHeaderRightRlay = (RelativeLayout) findViewById(R.id.rlay_header_rightfunc);
        mFinishBtn = (Button) findViewById(R.id.btn_me_finish);
        mCodeTv = (TextView) findViewById(R.id.tv_register_getverficode);
        mNewCodeEt = (EditText) findViewById(R.id.et_register_verification);
        mTimeTv = (TextView) findViewById(R.id.tv_me_time);

        mBackContentTv.setText("返回");
        mHeaderTitleTv.setText("手机验证码");
        mHeaderRightRlay.setVisibility(View.GONE);

        mBackLay.setOnClickListener(this);
        mFinishBtn.setOnClickListener(this);
        mCodeTv.setOnClickListener(this);


    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            recLen--;
            mTimeTv.setText("" + recLen);
            handler.postDelayed(this, 1000);
            if (recLen == 0) {
                mTimeTv.setVisibility(View.GONE);
                mCodeTv.setVisibility(View.VISIBLE);
                recLen = 60;
                mTimeTv.setText("" + recLen);
                handler.removeCallbacks(runnable);
                return;
            }
        }
    };


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.llay_header_back:
                finish();
                break;
            case R.id.tv_register_getverficode: //点击获取验证码,发送网络请求,获取验证码

                Intent intent1 = this.getIntent();
                newPhoneNumber = intent1.getStringExtra("newPhoneNumber");
                String realUrl = Url.SURL + "/Wedding/VerificationCode?Token=00&MobileNum=" + newPhoneNumber;
                httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, realUrl,
                        this, CommonResultBean.class, RequestCode.VERIFICATIONCODE);
                HttpHandleUtil.executeCommonPost(this, httpRequestBean);
                break;
            case R.id.btn_me_finish: //点击完成按钮进入新手机号绑定界面
                newCode = mNewCodeEt.getText().toString().trim();
                if (TextUtils.isEmpty(newCode)) {
                    CUtils.showToast("验证码不能为空");
                    return;
                } else if (verificationCode == null) {
                    CUtils.showToast("您还没有获取验证码,请先获取验证码");
                    return;
                } else if (!(verificationCode.equals(newCode))) {
                    CUtils.showToast("您输入的验证码有误,请重新输入");
                    return;
                }
                // 验证码发送之后修改绑定的手机号
                String url= UrlDealUtil.getRealUrl(RequestCode.UPDATE_MOBILENUM);
                HashMap<String,String> requestParams=new HashMap<String,String>();
                requestParams.put("MobileNum",""+newPhoneNumber);
                httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                        this, MeAlterPhoneBean.class, RequestCode.UPDATE_MOBILENUM, requestParams);
                HttpHandleUtil.executeCommonPost(this,httpRequestBean);
                break;

        }
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof CommonResultBean) {
            CommonResultBean commonData = (CommonResultBean) data;
            if ("0000".equals(commonData.getResCode())){
                handler.removeCallbacks(runnable);
                mCodeTv.setVisibility(View.GONE);
                mTimeTv.setVisibility(View.VISIBLE);
                handler.postDelayed(runnable,1000);
                Toast.makeText(MeNewSetBindnumActivity.this, "验证码已发送到您的手机,请注意查收", Toast.LENGTH_SHORT).show();
                verificationCode = commonData.getData();
            }
        }else if(data instanceof MeAlterPhoneBean) {
            ((GlobalApplication) getApplication()).getMeActivityManager().exitApp();
            //更新内存
            DataWareHourse.getInstance().getmUser().getData().get(0).setNvc_MobilePhone(newPhoneNumber);
            //更新本地缓存
            CacheUtils.saveObjectToTextFile(RequestCode.TOLOGIN_ACTIVITY_REQUESTCODE, DataWareHourse.getInstance().getmUser());
            Toast.makeText(MeNewSetBindnumActivity.this, "新手机号设置成功", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onError(ErrorBean errorBean) {
        Toast.makeText(MeNewSetBindnumActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
    }
}
